<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>快速排序</title>
  </head>
  <body>
    <script>

      // 以数组中某个值为基准值，然后分成数组，最后再把数组合并
      function quickSort(arr) {
        let len = arr.length;
        let left = []
        let right = []
        let temp = arr[0]
        for(let i=1;i<len;i++) {
            if(temp >= arr[i]){
              left.push(arr[i])
            }
            if(temp < arr[i]){
              right.push(arr[i])
            }
        }
        if(left.length > 1) {
            left = quickSort(left)
        }
        if(right.length > 1) {
            right = quickSort(right)
        }
        console.log(left.concat([temp]).concat(right))
        return left.concat([temp]).concat(right)
      }
      console.log(quickSort([3,4,5,2,1]))
    </script>
  </body>
</html>
